﻿package crochet.core.policy;

/*
 Copyright 2011 Intuitive Solutions

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
 */

import crochet.core.IExceptionHandlerDelegate;
import crochet.core.ISchedulerDelegate;
import crochet.core.configuration.CrochetConfiguration;
import crochet.core.configuration.CrochetConfigurationElement;
import crochet.core.configuration.RescheduleOnExceptionElement;
import crochet.core.configuration.RetryOnExceptionElement;

public class ExecutionPolicyFactory {
	private static ExecutionPolicyFactory epfInstance;

	public static ExecutionPolicyFactory getInstance() {
		if (epfInstance == null) {
			synchronized (ExecutionPolicyFactory.class) {
				if (epfInstance == null) {
					epfInstance = new ExecutionPolicyFactory();
				}
			}
		}

		return epfInstance;
	}/*
	 * 
	 * public IExecutionPolicy Create(string sSchedulerName, ExecutionPolicyType
	 * eptExecutionPolicyType, SchedulerDelegate sdSchedulerDelegate) { return
	 * Create(sSchedulerName, eptExecutionPolicyType, sdSchedulerDelegate,
	 * null); }
	 * 
	 * public IExecutionPolicy Create(string sSchedulerName, ExecutionPolicyType
	 * eptExecutionPolicyType, SchedulerDelegate sdSchedulerDelegate,
	 * ExceptionHandlerDelegate ehdExceptionHandlerDelegate) { return
	 * Create(sRootScheduler, eptExecutionPolicyType, sdSchedulerDelegate,
	 * ehdExceptionHandlerDelegate, GetParams(eptExecutionPolicyType,
	 * sPolicyName)); }
	 */

	public IExecutionPolicy Create(String sExecutionPolicyName,
			ISchedulerDelegate sdSchedulerDelegate) {
		return Create(sExecutionPolicyName, sdSchedulerDelegate, null);
	}

	public IExecutionPolicy Create(String sExecutionPolicyName,
			ISchedulerDelegate sdSchedulerDelegate,
			IExceptionHandlerDelegate ehdExceptionHandlerDelegate) {
		Object[] oaParams = null;
		IExecutionPolicy epExecutionPolicy = null;
		CrochetConfigurationElement cceCrochetConfigurationElement = CrochetConfiguration
				.getInstance().GetPolicyElement(sExecutionPolicyName);

		if (cceCrochetConfigurationElement != null) {
			oaParams = cceCrochetConfigurationElement.GetAttributes();
		}

		if (cceCrochetConfigurationElement instanceof RetryOnExceptionElement) {
			epExecutionPolicy = new RetryOnExceptionExecutionPolicy(null,
					sdSchedulerDelegate, ehdExceptionHandlerDelegate,
					(Integer) oaParams[1], (Integer) oaParams[2],
					(Boolean) oaParams[3]);
		} else if (cceCrochetConfigurationElement instanceof RescheduleOnExceptionElement) {
			epExecutionPolicy = new RescheduleOnExceptionExecutionPolicy(null,
					sdSchedulerDelegate, ehdExceptionHandlerDelegate,
					(Integer) oaParams[1], (Integer) oaParams[2],
					(Boolean) oaParams[3]);
		} else {
			epExecutionPolicy = new DefaultExecutionPolicy(null,
					sdSchedulerDelegate, ehdExceptionHandlerDelegate);
		}

		return epExecutionPolicy;
	}

	public Object[] GetParams(String sName) {
		Object[] oParams = null;

		if ("RetryOnException".equals(sName)) {
			oParams = CrochetConfiguration.getInstance()
					.getRetryOnExceptionPolicies(sName).GetAttributes();
		} else if ("RescheduleOnException".equals(sName)) {
			oParams = CrochetConfiguration.getInstance()
					.getRescheduleOnExceptionPolicies(sName).GetAttributes();
		}

		return oParams;
	}
}
